﻿#include"MainView.h"
#include"XQPortraitTreeDockWidget.h"
#include"XQGraphicsViewWidget.h"
#include"XQModifyUserInfoView.h"
#include"XQUserManagementView.h"
#include"ProductManagementView.h"
#include"InventoryManagementView.h"
#include"OrderManagementView.h"
#include<QMessageBox>
#include<QTreeWidgetItem>
MainView::MainView(QWidget* parent)
	:XQMainView(parent)
{
}

MainView::~MainView()
{
}

void MainView::login(const XQUserData& user)
{
	auto catalogue = this->catalogue();
	catalogue->setPortrait(user.portrait());
	catalogue->setUserState(UserState::Login);
	bool hidden = user.permission() == XQUserData::Permission::user;
	//if (user.permission() != XQUserData::Permission::user)
	{
		catalogue->setHiddenItem(XQTreeDockWidget::Item::UserManagement, hidden);
		catalogue->setHiddenItem(MainView::Catalogue::Inventory, hidden);
	}
	setWindowTitle(user.name() +" 欢迎你");
	emit loginSignals(user);
}

void MainView::logout()
{
	auto catalogue = this->catalogue();
	//catalogue->setPortrait(user.portrait());
	catalogue->setUserState(UserState::Logout);
	catalogue->setHiddenItem(XQTreeDockWidget::Item::UserManagement, true);
	setWindowTitle("欢迎使用");
	closeStackedWidget(View::UserInfo);
	closeStackedWidget(View::ModifyPassword);
	closeStackedWidget(View::UserManagement);
	closeStackedWidget(MainView::Catalogue::Product);
	closeStackedWidget(MainView::Catalogue::Inventory);
	closeStackedWidget(MainView::Catalogue::Order);
	emit logoutSignals();
}

void MainView::openUserInfoView(const XQUserData& user)
{
	if (stackedWidget(View::UserInfo)==nullptr)
	{
		auto widget = new XQModifyUserInfoView();
		setTranscribeWidget(View::UserInfo,widget);
		widget->init();
		widget->setUserData(user);
		widget->setVisible(XQModifyUserInfoView::Type::oldPasswordType, false);
		widget->setVisible(XQUserInfoView::Type::PasswordType, false);
		widget->setVisible(XQUserInfoView::Type::TwoPasswordType, false);
		widget->setVisible(XQUserInfoView::Type::VerifyType, false);
		widget->setReadOnly((int)XQUserInfoView::Type::AccountType, true);
		widget->setReadOnly((int)XQUserInfoView::Type::EmailType, true);
		showStackedWidget(View::UserInfo, widget, "用户信息");
		connect(widget, &XQModifyUserInfoView::modifyAccountSignals, [=] {emit modifyUserInfoSignals(widget->userData()); });
		////如果是自己更新目录头像
		//connect(widget, &XQModifyUserInfoView::modifyAccountSignals, [=](const XQUserData& modifyUser) {
		//	if (modifyUser.account() == user.account())
		//	{
		//		catalogue()->setPortrait(modifyUser.portrait());
		//	}
		//	});
	}
	showStackedWidget(View::UserInfo);
}

void MainView::openModifyPasswordView(const XQUserData& user)
{
	if (stackedWidget(View::ModifyPassword) == nullptr)
	{
		auto widget = new XQModifyUserInfoView();
		setTranscribeWidget(View::ModifyPassword, widget);
		widget->init();
		widget->setPortrait(user.portrait());
		widget->setAccount(user.account());
		//widget->setVisible(false);
		widget->setVisible(XQUserInfoView::Type::EmailType, false);
		widget->setVisible(XQUserInfoView::Type::PhoneType, false);
		widget->setVisible(XQUserInfoView::Type::NameType, false);
		widget->setVisible(XQUserInfoView::Type::GendType, false);
		widget->setVisible(XQUserInfoView::Type::VerifyType, false);
		widget->setVisible(XQUserInfoView::Type::PermissionType, false);
		widget->setVisible(XQUserInfoView::Type::MtimeType, false);
		widget->setVisible(XQUserInfoView::Type::CtimeType, false);
		widget->setVisible(XQUserInfoView::Type::UuidType, false);
		widget->setReadOnly((int)XQUserInfoView::Type::PortraitType, true);
		widget->setReadOnly((int)XQUserInfoView::Type::AccountType, true);
		//widget->setReadOnly(XQUserInfoView::Type::email, true);
		showStackedWidget(View::ModifyPassword, widget, "修改密码");
		connect(widget, &XQModifyUserInfoView::modifyAccountSignals, [=] {
			//auto newUser = widget->userData();
			if (widget->oldPassword() != user.password())
				QMessageBox::information(this, "修改密码", "旧密码输入错误");
			else if (widget->passwordEqual() == user.password())
				QMessageBox::information(this, "修改密码", "旧密码与新密码相同");
			else
				emit modifyPasswordSignals(widget->userData());
			
			});
	}
	showStackedWidget(View::ModifyPassword);
}


void MainView::openUserManagement(const XQUserData& user)
{
	if (stackedWidget(View::UserManagement) == nullptr)
	{
		auto widget = new XQUserManagementView();
		setTranscribeWidget(View::UserManagement, widget);
		widget->init();
		connect(widget, &XQUserManagementView::modifyUserViewCreateSignals, [=](XQModifyUserInfoView* view){
			auto per = view->currentPermission();
			if (per <= user.permission()&&view->userData().account()!=user.account())
			{//
				view->setReadOnly(/*XQUserInfoView::Type::PermissionType,*/ true);
				view->setReadOnly((int)XQUserInfoView::Type::CloseButtonType, false);
				view->setVisible(XQUserInfoView::Type::OkButtonType,false);
				return;
			}
			if (view->userData().account() == user.account())
			{
				view->setReadOnly((int)XQUserInfoView::Type::PermissionType, true);
			}
			else
			{
				auto p = user.permission<int>();
				QVariantMap map = XQUserData::Permission_toMap();
				for (int i = 0; i <= p; i++)
				{
					map.remove(map.key(i));
				}
				view->setPermission(map);
				view->setCurrentPermission(per);
			}
			});
		showStackedWidget(View::UserManagement, widget, "用户管理");
		connect(widget, &XQUserManagementView::addUserSignals,this, &MainView::addUserSignals);
		connect(widget, &XQUserManagementView::modifyUserSignals, this, &MainView::userManagementSignals);
		connect(widget, &XQUserManagementView::removeUserSignals, this, &MainView::removeUserSignals);
		
	}
	showStackedWidget(View::UserManagement);
	//return (XQUserManagementView*)stackedWidget(View::UserManagement);
}

void MainView::openProductManagement(const XQUserData& user)
{
	if (stackedWidget(Catalogue::Product) == nullptr)
	{
		auto View = new ProductManagementView();
		View->init();
		showStackedWidget(Catalogue::Product, View, "商品管理");
	}
	showStackedWidget(Catalogue::Product);
}

void MainView::openInventoryManagement(const XQUserData& user)
{
	if (stackedWidget(Catalogue::Inventory) == nullptr)
	{
		auto View = new InventoryManagementView();
		View->init();
		showStackedWidget(Catalogue::Inventory, View, "库存管理");
	}
	showStackedWidget(Catalogue::Inventory);
}

void MainView::openOrderManagement(const XQUserData& user)
{
	if (stackedWidget(Catalogue::Order) == nullptr)
	{
		auto View = new OrderManagementView();
		View->init();
		showStackedWidget(Catalogue::Order, View, "订单管理");

	}
	showStackedWidget(Catalogue::Order);
}

void MainView::init()
{
	XQMainView::init();
	//init_catalogue();

}

void MainView::init_catalogue()
{
	XQMainView::init_catalogue();
	auto catalogue = this->catalogue();
	catalogue->addTopLevelItem_User();
	//catalogue->addTopLevelItem_System();
	catalogue->setItemClickedFunc(XQTreeDockWidget::Item::Logout, [=] {logout(); });
	init_stashCatalogue();
}

void MainView::init_stashCatalogue()
{
	auto catalogue = this->catalogue();
	if (catalogue->treeWidgetItem(Catalogue::StashManage))
	{
		delete catalogue->treeWidgetItem(Catalogue::StashManage);
	}
	auto stash = new QTreeWidgetItem;
	catalogue->addTreeWidgetItem(Catalogue::StashManage, stash);
	stash->setText(0, "仓库管理");
	{
		QTreeWidgetItem* item = new QTreeWidgetItem(stash);
		catalogue->addTreeWidgetItem(Catalogue::Product, item);
		item->setText(0, "商品管理");
	}
	{
		QTreeWidgetItem* item = new QTreeWidgetItem(stash);
		catalogue->addTreeWidgetItem(Catalogue::Inventory, item);
		item->setText(0, "库存管理");
	}
	{
		QTreeWidgetItem* item = new QTreeWidgetItem(stash);
		catalogue->addTreeWidgetItem(Catalogue::Order, item);
		item->setText(0, "订单管理");
	}
	catalogue->addTopLevelItem(stash);
}
